﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using DTO;
using System.Collections;

namespace BLL
{
    public class NamHocBLL
    {
        static int id = 0;
        QuanLyDiemDataContext DB = new QuanLyDiemDataContext();
        public IQueryable SelectDes()
        {
            return DB.NamHocs.OrderByDescending(s =>s.MaNamHoc).AsQueryable();
        }
        public string Insert(NamHocDTO namhoc)
        {
            try
            {
                NamHoc nh = new NamHoc();
                nh.MaNamHoc = (id++).ToString();
                nh.TenNamHoc = namhoc.TenNamHoc;
                nh.TrangThai = namhoc.TrangThai;

                DB.NamHocs.InsertOnSubmit(nh);
                DB.SubmitChanges();
                return "Thanh cong";
            }
            catch (Exception ex)
            {
                return ex.Message; 
            }
        }
        public string Update(NamHocDTO namhoc)
        {
            try
            {
                NamHoc nh = DB.NamHocs.Where(s => s.MaNamHoc == namhoc.MaNamHoc).FirstOrDefault();
                nh.TenNamHoc = namhoc.TenNamHoc;
                nh.TrangThai = namhoc.TrangThai;
                DB.SubmitChanges();
                return "Thanh cong";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        public string Delete()
        {
            try
            {                
                DB.SubmitChanges();
                return "Thanh cong";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        public List<NamHocDTO> getNamHoc()
        {
            //lay het du lieu tu bang khoi
            List<NamHoc> list = DB.NamHocs.Where(s => s.TrangThai == "Chưa kết thúc").ToList<NamHoc>();
            //tao moi 1 doi tuong KhoiDTO de chua du lieu cua bang khoi
            List<NamHocDTO> listdto = new List<NamHocDTO>();
            foreach (NamHoc nam in list)
            {
                NamHocDTO k = new NamHocDTO();
                k.MaNamHoc = nam.MaNamHoc;
                k.TenNamHoc = nam.TenNamHoc;
                listdto.Add(k);
            }
            return listdto;
        }
        public string GetStatus(string manamhoc)
        {
            var namhoc = DB.NamHocs.Where(nam => nam.MaNamHoc == manamhoc).FirstOrDefault();
            return namhoc.TrangThai;
        }
        public bool check(string tennamhoc)
        {
            var kq = DB.NamHocs.Where(n => n.TenNamHoc == tennamhoc).ToList();
            int kt = 0;
            kt = kq.Count();
            if (kt == 0)
                return true;
            else return false;
        }
    }
}
